class ImportDataFromOldVersion < ActiveRecord::Migration
  def self.up
    sql = <<-SQL
      INSERT INTO drugs(id, name)
      SELECT id, name
      FROM sideffect_orig.drugs
      ORDER BY id ASC
    SQL
    execute(sql)

    sql = <<-SQL
      INSERT INTO ratings(id, drug_id, score, reason, sideffect_description, comment, sex, age, times_taken, days_taken, date_added)
      SELECT id, drug_id, score, reason, side_effect_text, comment, sex, age, times_taken, days_taken, date_added
      FROM sideffect_orig.ratings
      ORDER BY id ASC
    SQL
    execute(sql)
    
    sql = <<-SQL
      INSERT INTO categories(id, name)
      SELECT id, name
      FROM sideffect_orig.categories
      ORDER BY id ASC
    SQL
    execute(sql)
  
    sql = <<-SQL
      INSERT INTO categories_drugs(category_id, drug_id)
      SELECT category_id, drug_id
      FROM sideffect_orig.categories_drugs
      ORDER BY category_id ASC, drug_id ASC
    SQL
    execute(sql)
    
    sql = <<-SQL
      INSERT INTO sideffects(id, name)
      SELECT id, name
      FROM sideffect_orig.sideffects
      ORDER BY id ASC
    SQL
    execute(sql)
    
    sql = <<-SQL
      INSERT INTO ratings_sideffects(rating_id, sideffect_id)
      SELECT rating_id, sideffect_id
      FROM sideffect_orig.ratings_sideffects
      ORDER BY rating_id ASC, sideffect_id ASC
    SQL
    execute(sql)    
    
  end

  def self.down
    ['ratings_sideffects', 'sideffects', 'categories_drugs', 'categories', 'ratings', 'drugs'].each do |table|
      sql = <<-SQL
        DELETE FROM #{table}
      SQL
      execute(sql)
    end
  end
end
